<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Plus, Avatar } from '@element-plus/icons-vue'
import { regionData } from 'element-china-area-data'
import type { UploadProps } from 'element-plus'
import { mapSuggestion, Address, search } from '@/api/qqMap'

const router = useRouter()
const { path } = router.currentRoute.value

const activeIndex = path

const toUserPage = () => {
  router.push({
    name: 'user'
  })
}

const form = reactive({
  id: 7,
  name: 'index',
  position: '',
  user_name: '王*生',
  address: '',
  address_obj: '',
  detail_address: ''
})

const selectedOptions = ref()
const handleChange = async (value: {0: string, 1: string, 2: string}) => {
  let key = value[2]
  if (Number(value[2]) === Number(value[1]) + 1) {
    key = value[1]
  }
  const city = await search(key)
  region.value = city
  console.log(city)
}

const region = ref('杭州市')
const suggestionAddress = ref<Address | string>('西溪八方城')
const queryAddress = (queryString: string, cb: (arg: Address[]) => void) => {
  mapSuggestion({
    keyword: queryString,
    region: region.value
  }).then((res: Address[]) => {
    cb(res)
  }).catch(
    err => console.log(err)
  )
}
const selectAddress = (address: Address) => {
  form.address = address.title
  form.address_obj = JSON.stringify(address)
}

const onSubmit = () => { /**/ }

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

</script>
<template>

  <el-container>
      <el-header class="header">
        <div class="title">
          <img src="~@/assets/images/logo.png" alt="" class="title-logo">
          <span class="title-text">管理系统
            <el-divider class="divider" direction="vertical" />
          </span>
        </div>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          router
        >
          <el-menu-item index="/manage/daily/customerManage">每日配送</el-menu-item>
          <el-menu-item index="/manage/message/customerMessage">信息管理</el-menu-item>
        </el-menu>
        <div @click="toUserPage" class="user-info">
          <el-avatar :size="40" shape="square" src="https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png" />
          <div class="user-info-name">index</div>
        </div>
      </el-header>
      <el-container class="common-layout">
        <el-aside width="200px">
          <el-menu
            default-active="1"
            class="el-menu-vertical-demo"
          >
            <el-menu-item index="1">
              <el-icon><Avatar /></el-icon>
              <span>账号管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-container>
          <el-main class="main-container">
            <el-card shadow="Never">
              <el-form :model="form" label-width="80px" label-position="left" class="from">
                <el-form-item label="头像">
                  <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                  >
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                  </el-upload>
                  <!-- <el-avatar :size="80" shape="square" src="https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png" /> -->
                </el-form-item>
                <el-form-item label="账号 Id">
                  <el-input v-model="form.id" />
                </el-form-item>
                <el-form-item label="用户名">
                  <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="真实姓名">
                  <el-input v-model="form.user_name" />
                </el-form-item>
                <el-form-item label="奶站地区">
                  <el-cascader
                    placeholder="请选择地区"
                    size="default"
                    :options="regionData"
                    v-model="selectedOptions"
                    @change="handleChange"
                    style="width: 500px;"
                  ></el-cascader>
                </el-form-item>
                <el-form-item label="奶站地址" prop="address">
                  <el-autocomplete
                    v-model="suggestionAddress"
                    :fetch-suggestions="queryAddress"
                    @select="selectAddress"
                    value-key="title"
                  >
                  <template #default="{item}">
                      <span>{{ item.title }}</span>
                      <span class="suggestions-address">{{ item.address }}</span>
                    </template>
                  </el-autocomplete>
                </el-form-item>
                <el-form-item label="详细地址">
                  <el-input v-model="form.detail_address" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="onSubmit">保存</el-button>
                </el-form-item>
              </el-form>
            </el-card>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
</template>
<style lang="scss" scoped>

.header {
  padding: 0;
  display: flex;
  flex-wrap: nowrap;
  background-color: #fff;
  .title {
    width: 200px;
    display: flex;
    flex-wrap: nowrap;
    border-bottom: solid 1px #dcdfe6;
    // border-right: solid 1px #dcdfe6;
    &-logo {
      height: 59px;
    }
    &-text {
      margin-left: 20px;
      line-height: 59px;
      font-size: 18px;
      font-weight: bold;
    }
  }
  .el-menu-demo {
    flex: 1;
  }
  .user-info {
    border-bottom: solid 1px #dcdfe6;
    padding: 10px 0;
    width: 200px;
    &-name {
      display: inline-block;
      margin-left: 8px;
      font-size: 20px;
    }
  }

}

.common-layout {
  margin-top: 10px;
  width: calc(100% - 10px);
  margin-left: 10px;
  .el-menu-vertical-demo {
    border-radius: 4px;
    border:  1px solid #e4e7ed;
  }
  .main-container {
    width: calc(100% - 210px);
    padding: 0 10px;
  }
}

.from {
  width: 600px;
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
    border: solid 1px #eee;
  }
}

</style>
